package cn.wolfcode.car.business.web.controller.statementItem;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.service.StatementItemService;
import cn.wolfcode.car.business.service.StatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author WaveHi
 * @Date 15/11/2021 上午 8:58
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Controller
@RequestMapping("/business/statementItem")
public class StatementItemController {

    private final String prefix = "business/statementItem/";

    @Autowired
    private StatementItemService statementItemService;

    @Autowired
    private StatementService statementService;

    @RequiresPermissions("business:statementItem:list")
    @RequestMapping("/itemDetail")
    public String itemDetail(
            @RequestParam(value = "statementId", required = false) Long id,
            Model model) {
        Statement statement = statementService.getStatement(id);
        Integer status = statement.getStatus();
        model.addAttribute("statement", statement);
        if(status.equals(Statement.STATUS_PAID)) {
            return prefix + "showDetail";
        }
        return prefix + "list";
    }

    @RequiresPermissions("business:statementItem:query")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(
            @RequestParam(value = "pageNo",  required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required =  false) Integer pageSize,
            @RequestParam(value = "statementId", required = false) Long statementId) {
        //Judge data
        pageNo = pageNo != null ? pageNo : 1;
        pageSize = pageSize != null ? pageSize : 3;
        if( statementId != null) {
            //Query
            return statementItemService.getPartOfStatementItems(statementId);
        }
        return new TablePageInfo<>(new ArrayList<>());
    }

    @RequiresPermissions("business:statementItem:saveItem")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> statementItems) {
        statementItemService.addItems(statementItems);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:statementItem:payStatement")
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payment(
            @RequestParam(value = "statementId", required = false) Long statementId) {
        if(statementId != null) {
            statementItemService.pay(statementId);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
